package last.project;

import gameView.*;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FinalProjectActivity extends Activity {
    /** Called when the activity is first created. */
	private Context cxt;
	public static HighScoreSystem HSSystem;
	public static DifficultyManager DManager;
	public static int MAX_X, MAX_Y;
	public static int DEFAULT_BUTTON_TEXT_SIZE;
	
	private Button PlayButton, OptionButton, InstructionButton, HighScoreButton;
	
	public class HighScoreSystem {
		public ArrayList<String> Names = new ArrayList<String>();
		public ArrayList<Integer> Scores = new ArrayList<Integer>();
		public static final int Names_Stored = 10;
		private String curLine;
		
		public HighScoreSystem() {
			openHighScores();
		}
		public void openHighScores() {
			try {
				InputStream in = openFileInput("High_Scores.txt");
				if (in!=null) {
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					while ((curLine=reader.readLine())!=null) {
						Names.add(nextPart());
						Scores.add(Integer.parseInt(nextPart()));
					}
					reader.close();
				} else {
					throw new Exception();
				}
				in.close();
			} catch (Exception e) {
				
			}
		}
		public void addHighScore(String name, int score) {
			int place=1;
        	for (int i=0; i<Scores.size() && Scores.get(i)>=score; i++) {
        		place++;
        	}
        	Names.add(place-1, name);
        	Scores.add(place-1, score);
        	while (Names.size()>Names_Stored)
        		Names.remove(Names_Stored);
        	while (Scores.size()>Names_Stored) 
        		Scores.remove(Names_Stored);
			try {
				OutputStream out = openFileOutput("High_Scores.txt", 0);
				if (out!=null) {
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
					for (int i=0 ;i<Names.size() ;i++) {
						writer.write(Names.get(i)+","+Scores.get(i));
						writer.newLine();
					}
					writer.close();
				} else {
					throw new IOException();
				}
				out.close();
			} catch (IOException e) {
				Toast.makeText(cxt, "ERROR IN WRITING HIGH SCORES", Toast.LENGTH_LONG);
				e.printStackTrace();
			}
		}
		public void resetHighScores() {
			try {
				InputStream in = openFileInput("High_Scores_DEFAULT.txt");
				OutputStream out = openFileOutput("High_Scores.txt", 0);
				if (out!=null && in!=null) {
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
					String curLine;
					while ((curLine=reader.readLine())!=null) {
						writer.write(curLine);
						writer.newLine();
					}
					reader.close();
					writer.close();
				}
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		public void checkEmptyHighScores() {
			try {
				InputStream in = openFileInput("High_Scores.txt");
				if (in!=null) {
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					String curLine;
					if ((curLine=reader.readLine())==null) {
						resetHighScores();
					}
				}
			} catch (IOException e) {
				resetHighScores();
				e.printStackTrace();
			}
		}
		private String nextPart() {
			String ans="";
			while (curLine.length()>0 && !curLine.substring(0,1).equals(",")) {
				ans+=curLine.substring(0,1);
				curLine=curLine.substring(1);
			}
			if (curLine.length()>0) {
				curLine=curLine.substring(1);
			}
			return ans;
		}
	}
	public class DifficultyManager {
		public double difficulty=3.0;
		public static final double DIFFICULTY_BABY=1.0,
								   DIFFICULTY_EASY=3.0,
								   DIFFICULTY_MEDIUM=5.0,
								   DIFFICULTY_HARD=8.0,
								   DIFFICULTY_EXTREME=20.0;
		public ArrayList<EnemyInfo> enemies;
		public ArrayList<Double> enemyDifficultyLevel;
		
		public DifficultyManager() {
			
		}
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        
        cxt=this;

		WindowManager manager = (WindowManager) cxt.getSystemService(Context.WINDOW_SERVICE);
		MAX_X = manager.getDefaultDisplay().getWidth();
		MAX_Y = manager.getDefaultDisplay().getHeight();
		HSSystem = new HighScoreSystem();
		DManager=new DifficultyManager();
		
		HSSystem.checkEmptyHighScores();
		
		setupLayout();
        setupButtons();
    }
	private void setupLayout() {
		int BUTTON_TEXT_SIZE=DEFAULT_BUTTON_TEXT_SIZE;
		
		AbsoluteLayout layout = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout1);
		AbsoluteLayout.LayoutParams params;
		
		params = new AbsoluteLayout.LayoutParams(320, 80, MAX_X/2-160, MAX_Y/4);
		TextView Title = new TextView(cxt);
		Title.setText("Gravity Survival");
		Title.setTextSize(30);
		Title.setGravity(Gravity.CENTER_HORIZONTAL);
		layout.addView(Title, params);
		
		params = new AbsoluteLayout.LayoutParams(260, 65, MAX_X/2-120, MAX_Y/2);
		PlayButton = new Button(cxt);
		PlayButton.setText("Play!");
		PlayButton.setTextSize(BUTTON_TEXT_SIZE);
		layout.addView(PlayButton, params);
		
		params = new AbsoluteLayout.LayoutParams(260, 65, MAX_X/2-120, MAX_Y/2+70);
		OptionButton = new Button(cxt);
		OptionButton.setText("Options");
		OptionButton.setTextSize(BUTTON_TEXT_SIZE);
		layout.addView(OptionButton, params);
		
		params = new AbsoluteLayout.LayoutParams(260, 65, MAX_X/2-120, MAX_Y/2+140);
		InstructionButton = new Button(cxt);
		InstructionButton.setText("Instruction");
		InstructionButton.setTextSize(BUTTON_TEXT_SIZE);
		layout.addView(InstructionButton, params);
		
		params = new AbsoluteLayout.LayoutParams(260, 65, MAX_X/2-120, MAX_Y/2+210);
		HighScoreButton = new Button(cxt);
		HighScoreButton.setText("High Scores");
		HighScoreButton.setTextSize(BUTTON_TEXT_SIZE);
		layout.addView(HighScoreButton, params);
	}
	private void setupButtons() {
    	//listeners
        View.OnClickListener 
	        PlayButtonListener = new View.OnClickListener() {
	        	public void onClick(View v) {
	        		Intent tent = new Intent(cxt, GameScreen.class);
	        		
	        		cxt.startActivity(tent);
	        	}
	        },
	        OptionButtonListener = new View.OnClickListener() {
	        	public void onClick(View v) {
	        		Intent tent = new Intent(cxt, OptionScreen.class);
	        		cxt.startActivity(tent);
	        	}
	        },
	        InstructionButtonListener = new View.OnClickListener() {
	        	public void onClick(View v) {
	        		Intent tent = new Intent(cxt, InstructionScreen.class);
	        		cxt.startActivity(tent);
	        	}
	        },
	        HighScoreButtonListener = new View.OnClickListener() {
	        	public void onClick(View v) {
	        		Intent tent = new Intent(cxt, HighScoreScreen.class);
	        		cxt.startActivity(tent);
	        	}
	        };
        
        //attaching to buttons
	    PlayButton.setOnClickListener(PlayButtonListener);
	    OptionButton.setOnClickListener(OptionButtonListener);
	    InstructionButton.setOnClickListener(InstructionButtonListener);
	    HighScoreButton.setOnClickListener(HighScoreButtonListener);
	}
	
	
}